/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sigodonto.model.dao;

import br.com.sigodonto.model.entity.Clinica;
import br.com.sigodonto.model.entity.ItemEstoque;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Fidelis
 */
public class ItemEstoqueDao extends DaoGenerico {

    public ItemEstoqueDao() {
        super();
    }
    
    public boolean excluirItemEstoque(Clinica clinica, ItemEstoque itemEstoque){
         this.getEntityManager().getTransaction().begin();

        try {
            this.getEntityManager().refresh(clinica);
            List<ItemEstoque> itens = clinica.getItensEstoque();
            itens.remove(itemEstoque);
            clinica.setItensEstoque(itens);
            this.getEntityManager().remove(itemEstoque);
            this.getEntityManager().getTransaction().commit();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            this.getEntityManager().getTransaction().rollback();
            return false;
        }
    }
    
    public List<ItemEstoque> menorEstoque(){
        List<ItemEstoque> lista = null;
        this.getEntityManager().getTransaction().begin();

        try{
            Query q = this.getEntityManager().createQuery("SELECT t FROM ItemEstoque t ORDER BY t.quantidade_estoque ASC LIMIT 5");
            lista = (List<ItemEstoque>) q.getResultList();
            this.getEntityManager().getTransaction().commit();
        } catch (Exception ex){
            ex.printStackTrace();
            this.getEntityManager().getTransaction().rollback();
        }
        return lista;
    }
    
}
